package com.tozed.test;

import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

import com.cmcc.nativepackage.Printer;
import com.tozed.util.Util;
import com.tozed.widget.BadgeView;
import com.tozed.widget.MetroButton;
import com.tozed.widget.TitleBar;

public class MainActivity extends Base implements OnClickListener
{
	private final int COLOR_RED = R.color.red;
	private final int COLOR_GREEN = R.color.green;
	private final int STR_PASS_ID = R.string.str_test_pass;
	private final int STR_FAIL_ID = R.string.str_test_fail;
	private final int STR_UNTEST_ID = R.string.str_test_untest;
	
	private TitleBar mTitleBar;
	private MetroButton mBtnIdCard;
	private MetroButton mBtnSmCard;
	private MetroButton mBtnPrinter;
	private MetroButton mBtnCamera;
	private MetroButton mBtnMagneticcard;
	private MetroButton mBtnBluetooth;
	private MetroButton mBtnFm;
	private MetroButton mBtnSDCard;
	private MetroButton mBtnUsb;
	
	private MetroButton mBtnKeypad;
	private MetroButton mBtnRing;
	private MetroButton mBtnMore;
	
	private BadgeView mBadgeIdCard;
	private BadgeView mBadgeSmCard;
	private BadgeView mBadgePrinter;
	private BadgeView mBadgeCamera;
	private BadgeView mBadgeMagneticcard;
	
	private BadgeView mBadgeBluetooth;
	private BadgeView mBadgeFm;
	private BadgeView mBadgeSDCard;
	private BadgeView mBadgeUsb;
	
	private BadgeView mBadgeKeypad;
	private BadgeView mBadgeRing;
	
	private Button mBtnMultiTest;
	private Button mBtnPrintTestResult;
	
	private Map<String, MyTestResult> mSingleTestResults;
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		
		mTitleBar = (TitleBar)findViewById(R.id.title_bar);
		mTitleBar.setTiteText(R.string.app_name);
		mTitleBar.hideLeftButton();
		mTitleBar.hideRightButton();
		
		mSingleTestResults = FTApplication.getSingleResults();
		setupViews();
	}
	
	private void setupViews(){
		mBtnIdCard = (MetroButton)findViewById(R.id.btnIdcard);
		mBtnSmCard = (MetroButton)findViewById(R.id.btnSmCard);
		mBtnPrinter = (MetroButton)findViewById(R.id.btnPrinter);
		mBtnCamera = (MetroButton)findViewById(R.id.btnCamera);
		mBtnMagneticcard = (MetroButton)findViewById(R.id.btnMagneticcard);
		mBtnBluetooth = (MetroButton)findViewById(R.id.btnBluetooth);
		mBtnFm = (MetroButton)findViewById(R.id.btnFm);
		mBtnSDCard = (MetroButton)findViewById(R.id.btnSDCard);
		mBtnUsb = (MetroButton)findViewById(R.id.btnUsb);
		
		mBtnKeypad = (MetroButton)findViewById(R.id.btnKeypad);
		mBtnRing= (MetroButton)findViewById(R.id.btnRing);
		mBtnMore = (MetroButton)findViewById(R.id.btnMore);
		
		mBtnIdCard.setOnClickListener(this);
		mBtnSmCard.setOnClickListener(this);
		mBtnPrinter.setOnClickListener(this);
		mBtnCamera.setOnClickListener(this);
		mBtnMagneticcard.setOnClickListener(this);
		mBtnBluetooth.setOnClickListener(this);
		mBtnFm.setOnClickListener(this);
		mBtnSDCard.setOnClickListener(this);
		mBtnUsb.setOnClickListener(this);
		
		mBtnKeypad.setOnClickListener(this);
		mBtnRing.setOnClickListener(this);
		mBtnMore.setOnClickListener(this);
		
		mBtnMultiTest = (Button)findViewById(R.id.btnMutilTest);
		mBtnPrintTestResult = (Button)findViewById(R.id.btnPrintTestResult);
		mBtnMultiTest.setOnClickListener(this);
		mBtnPrintTestResult.setOnClickListener(this);
		
		// setup badges
		mBadgeIdCard = setupBadgeView(Constant.KEY_IDCARD, mBtnIdCard);
		mBadgeSmCard = setupBadgeView(Constant.KEY_SMCARD, mBtnSmCard);
		mBadgePrinter = setupBadgeView(Constant.KEY_PRINTER, mBtnPrinter);
		mBadgeCamera = setupBadgeView(Constant.KEY_CAMERA, mBtnCamera);
		mBadgeMagneticcard = setupBadgeView(Constant.KEY_MAGNETIC_CARD, mBtnMagneticcard);
		mBadgeBluetooth = setupBadgeView(Constant.KEY_BLUETOOTH, mBtnBluetooth);
		mBadgeFm = setupBadgeView(Constant.KEY_FM, mBtnFm);
		mBadgeSDCard = setupBadgeView(Constant.KEY_SDCARD, mBtnSDCard);
		mBadgeUsb = setupBadgeView(Constant.KEY_USB, mBtnUsb);
		
		mBadgeKeypad = setupBadgeView(Constant.KEY_KEYPAD, mBtnKeypad);
		mBadgeRing = setupBadgeView(Constant.KEY_RING, mBtnRing);
	}
	
	private BadgeView setupBadgeView(String key, View target){
		int nTestStatus;
		String strBadgeTip;
		int nColorBg;
		MyTestResult testResult;
		
		BadgeView badgView;
		
		testResult = mSingleTestResults.get(key);
		nTestStatus = testResult.passTimes;
		nColorBg = getResources().getColor(nTestStatus == 1 ? COLOR_GREEN : COLOR_RED);
		if (testResult.testTimes == 0)
			strBadgeTip = Util.getResString(STR_UNTEST_ID);
		else
			strBadgeTip = Util.getResString(nTestStatus == 1 ? STR_PASS_ID : STR_FAIL_ID);
		
		badgView = new BadgeView(this, target);
		badgView.setTextColor(Color.WHITE);
		badgView.setBadgeBackgroundColor(nColorBg);
		badgView.setText(strBadgeTip);
		badgView.show(true);
		
		return badgView;
	}
	
	@Override
	protected void onResume()
	{
		super.onResume();
		
		// 重新判断是否已测试
		
	}

	@Override
	public void onClick(View v)
	{
		Intent intent;
		
		switch (v.getId())
		{
		case R.id.btnIdcard:
			//startActivity(new Intent(MainActivity.this, IDCardTestActivity.class));
			intent = new Intent(MainActivity.this, IDCardTestActivity.class);
			intent.putExtra("TEST_TYPE", Constant.TYPE_TEST_SINGLE);
			startActivityForResult(intent, Constant.REQUEST_IDCARD);
			break;
			
		case R.id.btnSmCard:
			intent = new Intent(MainActivity.this, SMCardTestActivity.class);
			intent.putExtra("TEST_TYPE", Constant.TYPE_TEST_SINGLE);
			startActivityForResult(intent, Constant.REQUEST_SMCARD);
			break;
			
		case R.id.btnPrinter:
			intent = new Intent(MainActivity.this, PrinterTestActivity.class);
			intent.putExtra("TEST_TYPE", Constant.TYPE_TEST_SINGLE);
			startActivityForResult(intent, Constant.REQUEST_PRINTER);
			break;
			
		case R.id.btnCamera:
			intent = new Intent(MainActivity.this, CameraTestActivity.class);
			intent.putExtra("TEST_TYPE", Constant.TYPE_TEST_SINGLE);
			startActivityForResult(intent, Constant.REQUEST_CAMERA);
			break;
		
		case R.id.btnMagneticcard:
			intent = new Intent(MainActivity.this, MagneticCardTestActivity.class);
			intent.putExtra("TEST_TYPE", Constant.TYPE_TEST_SINGLE);
			startActivityForResult(intent, Constant.REQUEST_MAGNETIC_CARD);
			break;
			
		case R.id.btnBluetooth:
			intent = new Intent(MainActivity.this, BluetoothTestActivity.class);
			intent.putExtra("TEST_TYPE", Constant.TYPE_TEST_SINGLE);
			startActivityForResult(intent, Constant.REQUEST_BLUETOOTH);
			break;
			
		case R.id.btnFm:
			//intent = new Intent(MainActivity.this, FmTestActivity.class);
			intent = new Intent(MainActivity.this, FmTest2Activity.class);
			intent.putExtra("TEST_TYPE", Constant.TYPE_TEST_SINGLE);
			startActivityForResult(intent, Constant.REQUEST_FM);
			break;
		
		case R.id.btnSDCard:
			intent = new Intent(MainActivity.this, SDCardTestActivity.class);
			intent.putExtra("TEST_TYPE", Constant.TYPE_TEST_SINGLE);
			startActivityForResult(intent, Constant.REQUEST_SDCARD);
			break;
			
		case R.id.btnUsb:
			intent = new Intent(MainActivity.this, UsbTestActivity.class);
			intent.putExtra("TEST_TYPE", Constant.TYPE_TEST_SINGLE);
			startActivityForResult(intent, Constant.REQUEST_USB);
			break;
			
		case R.id.btnMore:
			intent = new Intent(MainActivity.this, MoreTestModuleActivity.class);
			//startActivity(intent);
			startActivityForResult(intent, Constant.REQUEST_MOTE_MODULE_TEST);
			break;
			
		case R.id.btnMutilTest:
			intent = new Intent(MainActivity.this, MultiTestChooseActivity.class);
			intent.putExtra("TEST_TYPE", Constant.TYPE_TEST_MULTI);
			startActivityForResult(intent, Constant.REQUEST_MULTI_TEST);
			break;
			
		case R.id.btnPrintTestResult:
			printMultiTestResult();
			break;
			
		case R.id.btnKeypad:
			intent = new Intent(MainActivity.this, KeypadTestActivity.class);
			intent.putExtra("TEST_TYPE", Constant.TYPE_TEST_SINGLE);
			startActivityForResult(intent, Constant.REQUEST_KEYPAD);
			break;
			
		case R.id.btnRing:
			intent = new Intent(MainActivity.this, RingTestActivity.class);
			intent.putExtra("TEST_TYPE", Constant.TYPE_TEST_SINGLE);
			startActivityForResult(intent, Constant.REQUEST_RING);
			break;
			
		default:
			break;
		}
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data)
	{
		super.onActivityResult(requestCode, resultCode, data);
		
		if (RESULT_OK != resultCode)
			return;
		
		int nTestStatus = data.getIntExtra("KEY_TEST_STATUS",0);
		int colorBg = getResources().getColor(nTestStatus == 1 ? COLOR_GREEN : COLOR_RED);
		String strText = Util.getResString(nTestStatus == 1 ? STR_PASS_ID : STR_FAIL_ID);
		
		//Map<String, TestResult> testResults = FTApplication.getSingleResults();
		
		MyTestResult value;
		switch (requestCode){
		case Constant.REQUEST_IDCARD:
			mBadgeIdCard.setTextColor(Color.WHITE);
			mBadgeIdCard.setBadgeBackgroundColor(colorBg);
			mBadgeIdCard.setText(strText);
			mBadgeIdCard.show(1);
			
			value = mSingleTestResults.get(Constant.KEY_IDCARD);
			if (value != null){
				value.testTimes = 1;
				value.passTimes = (nTestStatus == 1 ? 1 : 0);
				value.failTimes = (nTestStatus == 0 ? 1 : 0);
				FTApplication.setSingleResult(Constant.KEY_IDCARD, value);
			}
			
			break;
			
		case Constant.REQUEST_SMCARD:
			mBadgeSmCard.setTextColor(Color.WHITE);
			mBadgeSmCard.setBadgeBackgroundColor(colorBg);
			mBadgeSmCard.setText(strText);
			mBadgeSmCard.show(1);
			
			value = mSingleTestResults.get(Constant.KEY_SMCARD);
			if (value != null){
				value.testTimes = 1;
				value.passTimes = (nTestStatus == 1 ? 1 : 0);
				value.failTimes = (nTestStatus == 0 ? 1 : 0);
				FTApplication.setSingleResult(Constant.KEY_SMCARD, value);
			}
			break;
			
		case Constant.REQUEST_PRINTER:
			mBadgePrinter.setTextColor(Color.WHITE);
			mBadgePrinter.setBadgeBackgroundColor(colorBg);
			mBadgePrinter.setText(strText);
			mBadgePrinter.show(1);
			
			value = mSingleTestResults.get(Constant.KEY_PRINTER);
			if (value != null){
				value.testTimes = 1;
				value.passTimes = (nTestStatus == 1 ? 1 : 0);
				value.failTimes = (nTestStatus == 0 ? 1 : 0);
				FTApplication.setSingleResult(Constant.KEY_PRINTER, value);
			}
			break;
			
		case Constant.REQUEST_CAMERA:
			mBadgeCamera.setTextColor(Color.WHITE);
			mBadgeCamera.setBadgeBackgroundColor(colorBg);
			mBadgeCamera.setText(strText);
			mBadgeCamera.show(1);
			
			value = mSingleTestResults.get(Constant.KEY_CAMERA);
			if (value != null){
				value.testTimes = 1;
				value.passTimes = (nTestStatus == 1 ? 1 : 0);
				value.failTimes = (nTestStatus == 0 ? 1 : 0);
				FTApplication.setSingleResult(Constant.KEY_CAMERA, value);
			}
			break;
			
		case Constant.REQUEST_MAGNETIC_CARD:
			mBadgeMagneticcard.setTextColor(Color.WHITE);
			mBadgeMagneticcard.setBadgeBackgroundColor(colorBg);
			mBadgeMagneticcard.setText(strText);
			mBadgeMagneticcard.show(1);
			
			value = mSingleTestResults.get(Constant.KEY_MAGNETIC_CARD);
			if (value != null){
				value.testTimes = 1;
				value.passTimes = (nTestStatus == 1 ? 1 : 0);
				value.failTimes = (nTestStatus == 0 ? 1 : 0);
				FTApplication.setSingleResult(Constant.KEY_MAGNETIC_CARD, value);
			}
			break;
			
		case Constant.REQUEST_BLUETOOTH:
			mBadgeBluetooth.setTextColor(Color.WHITE);
			mBadgeBluetooth.setBadgeBackgroundColor(colorBg);
			mBadgeBluetooth.setText(strText);
			mBadgeBluetooth.show(1);
			
			value = mSingleTestResults.get(Constant.KEY_BLUETOOTH);
			if (value != null){
				value.testTimes = 1;
				value.passTimes = (nTestStatus == 1 ? 1 : 0);
				value.failTimes = (nTestStatus == 0 ? 1 : 0);
				FTApplication.setSingleResult(Constant.KEY_BLUETOOTH, value);
			}
			break;
			
		case Constant.REQUEST_FM:
			mBadgeBluetooth.setTextColor(Color.WHITE);
			mBadgeBluetooth.setBadgeBackgroundColor(colorBg);
			mBadgeBluetooth.setText(strText);
			mBadgeBluetooth.show(1);
			
			value = mSingleTestResults.get(Constant.KEY_FM);
			if (value != null){
				value.testTimes = 1;
				value.passTimes = (nTestStatus == 1 ? 1 : 0);
				value.failTimes = (nTestStatus == 0 ? 1 : 0);
				FTApplication.setSingleResult(Constant.KEY_FM, value);
			}
			break;
			
		case Constant.REQUEST_SDCARD:
			mBadgeSDCard.setTextColor(Color.WHITE);
			mBadgeSDCard.setBadgeBackgroundColor(colorBg);
			mBadgeSDCard.setText(strText);
			mBadgeSDCard.show(1);
			
			value = mSingleTestResults.get(Constant.KEY_SDCARD);
			if (value != null){
				value.testTimes = 1;
				value.passTimes = (nTestStatus == 1 ? 1 : 0);
				value.failTimes = (nTestStatus == 0 ? 1 : 0);
				FTApplication.setSingleResult(Constant.KEY_SDCARD, value);
			}
			break;
			
		case Constant.REQUEST_USB:
			mBadgeUsb.setTextColor(Color.WHITE);
			mBadgeUsb.setBadgeBackgroundColor(colorBg);
			mBadgeUsb.setText(strText);
			mBadgeUsb.show(1);
			
			value = mSingleTestResults.get(Constant.KEY_USB);
			if (value != null){
				value.testTimes = 1;
				value.passTimes = (nTestStatus == 1 ? 1 : 0);
				value.failTimes = (nTestStatus == 0 ? 1 : 0);
				FTApplication.setSingleResult(Constant.KEY_USB, value);
			}
			break;
			
		case Constant.REQUEST_MULTI_TEST:
			Log.e(Constant.TAG, "multi test finish.");
			dumpMultiTestResult();
			break;
			
		case Constant.REQUEST_KEYPAD:
			mBadgeKeypad.setTextColor(Color.WHITE);
			mBadgeKeypad.setBadgeBackgroundColor(colorBg);
			mBadgeKeypad.setText(strText);
			mBadgeKeypad.show(1);
			
			value = mSingleTestResults.get(Constant.KEY_KEYPAD);
			if (value != null){
				value.testTimes = 1;
				value.passTimes = (nTestStatus == 1 ? 1 : 0);
				value.failTimes = (nTestStatus == 0 ? 1 : 0);
				FTApplication.setSingleResult(Constant.KEY_KEYPAD, value);
			}
			break;
			
		case Constant.REQUEST_RING:
			mBadgeRing.setTextColor(Color.WHITE);
			mBadgeRing.setBadgeBackgroundColor(colorBg);
			mBadgeRing.setText(strText);
			mBadgeRing.show(1);
			
			value = mSingleTestResults.get(Constant.KEY_RING);
			if (value != null){
				value.testTimes = 1;
				value.passTimes = (nTestStatus == 1 ? 1 : 0);
				value.failTimes = (nTestStatus == 0 ? 1 : 0);
				FTApplication.setSingleResult(Constant.KEY_RING, value);
			}
			break;
			
		case Constant.REQUEST_MOTE_MODULE_TEST:
			//String strArrKeys[] = data.getStringArrayExtra("MORE_TEST_KEY");
			//updateMoreTestResults();
			break;
		default:
			break;
		}
	}
	
	private void updateMoreTestResults()
	{
		
	}
	
	private void printMultiTestResult(){
		Map<String, MyTestResult> multiTestResults = FTApplication.getMultiResults();
		Iterator<Entry<String, MyTestResult>> it = multiTestResults.entrySet().iterator();
		StringBuilder sb = new StringBuilder();
		
		for (;it.hasNext();){
			Entry<String, MyTestResult> element = it.next();
			String key = element.getKey();
			MyTestResult tr = element.getValue();
			
			if (tr.testTimes > 0){
				String strPrintItem = String.format("模块: %s\n测试次数: %d\n成功: %d 失败: %d", 
						Constant.getTestKeyString(tr.itemName),tr.testTimes, tr.passTimes, tr.failTimes);
				sb.append(strPrintItem);
				sb.append("\n-----------------\n");
			}
		}
		
		// print
		final String strPrintContent = sb.toString();
		if (TextUtils.isEmpty(strPrintContent)){
			Toast.makeText(this, "请先做" + Util.getResString(R.string.str_test_multi_times), 
					Toast.LENGTH_SHORT).show();
			return;
		}
		
		Printer.initialPrinter();
		Printer.openPrinter(1, "", "");
		Printer.setChroma(7);
		new Thread(){
			public void run() {
				Printer.printLogo();
				Printer.print("\n\n\n\n");
				Printer.print(strPrintContent);
				Printer.print("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
		}}.start();
	}
	
	private void dumpMultiTestResult(){
		Map<String, MyTestResult> multiTestResults = FTApplication.getMultiResults();
		Iterator<Entry<String, MyTestResult>> it = multiTestResults.entrySet().iterator();
		for (;it.hasNext();){
			Entry<String, MyTestResult> element = it.next();
			String key = element.getKey();
			MyTestResult testResut = element.getValue();
			
			String log = String.format("TestResult, key=[%s], name=%s, testTimes=%d, pass=%d, fail=%d", key, 
					testResut.itemName, testResut.testTimes, testResut.passTimes, testResut.failTimes);
			Log.e(Constant.TAG, log);
		}
	}
}
